package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;

/**
 * 商品详情服务service的实现类
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;
    /**
     * 根据skuId查询sku的详细信息,用于生成sku的详情页面
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getSkuDetail(Long skuId) {
        Map<String,Object> result = new HashMap<>();
        //查询sku信息
        CompletableFuture<SkuInfo> skuInfoCompletableFuture =
                CompletableFuture.supplyAsync(()->{
                    //远程调用商品微服务service-product，获取sku的info image 销售属性 平台属性信息
                    SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
                    //设置值
                    result.put("skuInfo",skuInfo);
                    //返回结果
                    return skuInfo;
                },threadPoolExecutor);
        //获取分类信息
        CompletableFuture<Void> categoryViewCompletableFuture =
                skuInfoCompletableFuture.thenAcceptAsync(new Consumer<SkuInfo>() {
                    @Override
                    public void accept(SkuInfo skuInfo) {

                        //类别
                        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
                        result.put("categoryView",categoryView);
                    }
                },threadPoolExecutor);
        //获取价格信息
        CompletableFuture<Void> priceCompletableFuture =
                skuInfoCompletableFuture.thenAcceptAsync(new Consumer<SkuInfo>() {
                    @Override
                    public void accept(SkuInfo skuInfo) {
                        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
                        result.put("price",skuPrice);
                    }
                },threadPoolExecutor);



        CompletableFuture<Void> spuSaleAttrListCompletableFuture =
                skuInfoCompletableFuture.thenAcceptAsync(new Consumer<SkuInfo>() {
                    @Override
                    public void accept(SkuInfo skuInfo) {
                        List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
                        result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
                    }
                },threadPoolExecutor);
        //获取所有销售属性值与sku的键值对
        CompletableFuture<Void> skuSaleAttrListBySpuIdCompletableFuture =
                skuInfoCompletableFuture.thenAcceptAsync(new Consumer<SkuInfo>() {
                    @Override
                    public void accept(SkuInfo skuInfo) {
                        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
                        //map类型最好进行json串转换,方便前端进行处理
                        result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
                    }
                },threadPoolExecutor);
        //等待所有任务完成
        CompletableFuture.allOf(skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuSaleAttrListCompletableFuture,
                skuSaleAttrListBySpuIdCompletableFuture).join();
        //商品浏览一次，hotscore + 1
        //异步更新热度值
        CompletableFuture.runAsync(()->{
            //更新热度值
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);
        //返回结果
        return result;
    }
}
